package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.entity.UserLearningProgress;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserLearningProgressMapper {
    
    @Select("SELECT * FROM user_learning_progress WHERE user_id = #{userId}")
    List<UserLearningProgress> findByUserId(@Param("userId") Long userId);
    
    @Select("SELECT * FROM user_learning_progress WHERE user_id = #{userId} AND course_id = #{courseId}")
    UserLearningProgress findByUserIdAndCourseId(@Param("userId") Long userId, @Param("courseId") Long courseId);
    
    @Select("SELECT p.*, c.course_name FROM user_learning_progress p " +
            "INNER JOIN courses c ON p.course_id = c.course_id " +
            "WHERE p.user_id = #{userId}")
    @Results({
        @Result(property = "progressId", column = "progress_id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "completionRate", column = "completion_rate"),
        @Result(property = "totalStudyTime", column = "total_study_time"),
        @Result(property = "status", column = "status"),
        @Result(property = "lastStudyTime", column = "last_study_time"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    List<Map<String, Object>> findProgressWithCourseInfo(@Param("userId") Long userId);
    
    @Insert("INSERT INTO user_learning_progress (user_id, course_id, completion_rate, total_study_time, status, create_time) " +
            "VALUES (#{userId}, #{courseId}, #{completionRate}, #{totalStudyTime}, #{status}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "progressId")
    void insert(UserLearningProgress progress);
    
    @Update("UPDATE user_learning_progress SET completion_rate = #{completionRate}, " +
            "total_study_time = #{totalStudyTime}, status = #{status}, last_study_time = #{lastStudyTime}, " +
            "update_time = NOW() " +
            "WHERE progress_id = #{progressId}")
    void update(UserLearningProgress progress);
    
    @Select("SELECT COUNT(*) FROM user_practices WHERE user_id = #{userId} AND course_id = #{courseId}")
    int countPracticesByUserAndCourse(@Param("userId") Long userId, @Param("courseId") Long courseId);
    
    @Select("SELECT COUNT(*) FROM user_exams e " +
            "INNER JOIN exam_papers p ON e.paper_id = p.paper_id " +
            "WHERE e.user_id = #{userId} AND p.course_id = #{courseId}")
    int countExamsByUserAndCourse(@Param("userId") Long userId, @Param("courseId") Long courseId);
    
    @Select("SELECT AVG(e.user_score * 1.0 / e.total_score * 100) FROM user_exams e " +
            "INNER JOIN exam_papers p ON e.paper_id = p.paper_id " +
            "WHERE e.user_id = #{userId} AND p.course_id = #{courseId}")
    Double getAverageScoreByUserAndCourse(@Param("userId") Long userId, @Param("courseId") Long courseId);
    
    /**
     * 获取用户所有的课程ID
     * 首先从学习进度表中获取，如果没有则从练习和考试记录中获取
     */
    @Select("SELECT DISTINCT course_id FROM user_learning_progress WHERE user_id = #{userId}")
    List<Long> getCourseIdsByUserId(@Param("userId") Long userId);
    
    /**
     * 从练习记录中获取用户的课程ID
     */
    @Select("SELECT DISTINCT course_id FROM user_practices WHERE user_id = #{userId}")
    List<Long> getCourseIdsFromPractices(@Param("userId") Long userId);
    
    /**
     * 从考试记录中获取用户的课程ID
     */
    @Select("SELECT DISTINCT p.course_id FROM exam_papers p " +
            "INNER JOIN user_exams e ON p.paper_id = e.paper_id " +
            "WHERE e.user_id = #{userId}")
    List<Long> getCourseIdsFromExams(@Param("userId") Long userId);
} 